using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ORSLibrary.Service;
using ORSLibrary.Common;
using System.Web;

namespace ORSLibrary.DAL
{
	public partial class ScheduleHotel : BusinessEntity
	{
        #region Public Properties
		
        public int ID { get; set; }
		public int CompanyID { get; set; }
		public int ProgramID { get; set; }
		public int ScheduleID { get; set; }
		public int HotelID { get; set; }
		public int AvailableCount { get; set; }
		public decimal Price { get; set; }
		public int CreatedBy { get; set; }
		public DateTime CreatedDate { get; set; }
		public int ModifiedBy { get; set; }
		public DateTime ModifiedDate { get; set; }
		public bool IsDeleted { get; set; }
		
        #endregion
        
		public ScheduleHotel()
		{
            IsNew = true;
		}
		
		public ScheduleHotel(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
			SqlDataReader reader = sql.ExecuteSPReader("ScheduleHotelSelect");
			
			if (reader.Read()) 
			{
				this.LoadFromReader(reader);
				reader.Close();
			}
			else
			{
				if (!reader.IsClosed) reader.Close();
				throw new ApplicationException("ScheduleHotel does not exist.");
			}
		}
		
		public ScheduleHotel(SqlDataReader reader)
		{
			this.LoadFromReader(reader);
		}
		
		protected void LoadFromReader(SqlDataReader reader)
		{
			if (reader != null && !reader.IsClosed)
			{
				ID = (int)reader["ID"];
                
				CompanyID = reader["CompanyID"] == DBNull.Value ? 0 : (int)reader["CompanyID"];
				ProgramID = reader["ProgramID"] == DBNull.Value ? 0 : (int)reader["ProgramID"];
				ScheduleID = reader["ScheduleID"] == DBNull.Value ? 0 : (int)reader["ScheduleID"];
				HotelID = reader["HotelID"] == DBNull.Value ? 0 : (int)reader["HotelID"];
				AvailableCount = reader["AvailableCount"] == DBNull.Value ? 0 : (int)reader["AvailableCount"];
				Price = reader["Price"] == DBNull.Value ? 0 : (decimal)reader["Price"];
				CreatedBy = reader["CreatedBy"] == DBNull.Value ? 0 : (int)reader["CreatedBy"];
				CreatedDate = reader["CreatedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["CreatedDate"];
				ModifiedBy = reader["ModifiedBy"] == DBNull.Value ? 0 : (int)reader["ModifiedBy"];
				ModifiedDate = reader["ModifiedDate"] == DBNull.Value ? DateTime.MinValue : (DateTime)reader["ModifiedDate"];
				IsDeleted = reader["IsDeleted"] == DBNull.Value ? false : (bool)reader["IsDeleted"];
                
                IsNew = false;
			}
		}
		
		public void Delete()
		{
			ScheduleHotel.Delete(ID);
		}
        
        public void Save()
		{
            if(IsNew)
                Create();
            else
                Update();
        }
		
		public void Update()
		{
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];

            SqlService sql = new SqlService();

			sql.AddParameter("@ID", SqlDbType.Int, ID);			
            FillParamters(sql);
		
			sql.ExecuteSP("ScheduleHotelUpdate");
		}
		
		public void Create()
		{
            CreatedBy = (int)HttpContext.Current.Session["UserID"];
            ModifiedBy = (int)HttpContext.Current.Session["UserID"];
            
			SqlService sql = new SqlService();

            FillParamters(sql);
			
			sql.ExecuteSP("ScheduleHotelInsert");
            
            ID = (int)sql.Parameters["@ID"].Value;
		}
        
        public void FillParamters(SqlService sql)
		{
			sql.AddParameter("@CompanyID", SqlDbType.Int, CompanyID);
			sql.AddParameter("@ProgramID", SqlDbType.Int, ProgramID);
			sql.AddParameter("@ScheduleID", SqlDbType.Int, ScheduleID);
			sql.AddParameter("@HotelID", SqlDbType.Int, HotelID);
			sql.AddParameter("@AvailableCount", SqlDbType.Int, AvailableCount);
			sql.AddParameter("@Price", SqlDbType.Money, Price);
			sql.AddParameter("@CreatedBy", SqlDbType.Int, CreatedBy);
			sql.AddParameter("@CreatedDate", SqlDbType.DateTime, CreatedDate);
			sql.AddParameter("@ModifiedBy", SqlDbType.Int, ModifiedBy);
			sql.AddParameter("@ModifiedDate", SqlDbType.DateTime, ModifiedDate);
			sql.AddParameter("@IsDeleted", SqlDbType.Bit, IsDeleted);
        }
		
		public static ScheduleHotel NewEntity(int ID)
		{
			ScheduleHotel newEntity = new ScheduleHotel();
			newEntity.ID = ID;

			return newEntity;
		}
		
        public static ScheduleHotel GetEntity(int ID)
		{
			return new ScheduleHotel(ID);
		}
		
		public static void Delete(int ID)
		{
			SqlService sql = new SqlService();
			sql.AddParameter("@ID", SqlDbType.Int, ID);
            sql.AddParameter("@ModifiedBy", SqlDbType.Int, HttpContext.Current.Session["UserID"]);
	
			sql.ExecuteSP("ScheduleHotelDelete");
		}
        
        public static ScheduleHotelList GetList(bool isComboBox)
		{
			ScheduleHotelList list = new ScheduleHotelList();
			
            SqlService sql = new SqlService();
			SqlDataReader reader = sql.ExecuteSPReader("ScheduleHotelSelectAll");
			
			while (reader.Read()) 
			{
                list.Add(new ScheduleHotel(reader));
			}
            reader.Close();
            
			return list;
		}
	}
    public partial class ScheduleHotelList : List<ScheduleHotel>
	{
    }
}
